#!/bin/python

#
#Xithyl, Test Driven Development for C
#Copyright (C) 2012  Stepan Henek
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from xithyl import logger
from xithyl.Ctypes.basic import *
from xithyl.Ctypes import libc
from xithyl.conditions import basic
from xithyl import actions

logger.addLogger(__name__)

def functional1():
    """
    Creates two arrays.
    Copy smaller into bigger.
    No overlap
    Compare the result.
    """

    result = True

    logger.log("Functional 1", __name__)

    logger.log("Init array1", __name__)

    str1 = makeArray(30, 0x0, 0x01, 0x02)

    # Test assignment
    result &= basic.arrayEqualsLen([0, 1, 2] * 10, charArrayToList(str1, 30), 30, __name__, True)

    logger.log("Init array2", __name__)

    str2 = makeArray(20, 0x03, 0x04)

    # Test assignment
    result &= basic.arrayEqualsLen([3, 4] * 10, charArrayToList(str2, 20), 20, __name__, True)

    logger.log("Copy arrays %x %x" % (getReferencedAddress(str1), getReferencedAddress(str2)), __name__)

    # Call memcpy
    target = libc.string.memcpy(str1, str2, 18)

    #Return value should be pointer to dest
    result &= basic.numbersEquals(getReferencedAddress(str1), getReferencedAddress(target), __name__, True)

    logger.log("Copied into %x" % getReferencedAddress(target), __name__)

    result &= basic.arrayEqualsLen([3, 4] * 9  + [0, 1, 2] * 4, charArrayToList(str1, 30), 30, __name__, True)

    logger.log("Functional 1 done", __name__)

    return result

# Note: overlaping is undefied 
